package com.crack.nga2.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.crack.nga2.R;
import com.crack.nga2.custom.PullListView;
import com.crack.nga2.entity.Message;
import com.crack.nga2.utils.GlobalConst;
import com.crack.nga2.utils.TimeUtils;
import com.crack.nga2.utils.HttpParams;
import com.crack.nga2.volley.MessageRequest;

import java.util.ArrayList;
import java.util.List;

public class MessageActivity extends BaseActivity implements Animation.AnimationListener, PullListView.OnRefreshListener, AdapterView.OnItemClickListener {
    private TextView tvPrivate, tvMessage;
    private PullListView listView;
    private int index = 0;
    private boolean flag;
    private int page = 1;
    private List<Message> messages = new ArrayList<>();
    private MessageAdapter adapter = new MessageAdapter();
    private boolean nextPage = true;
    private RequestQueue mQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        mQueue = NgaApplication.getQueue();
        tvMessage = (TextView) findViewById(R.id.tv_message);
        tvPrivate = (TextView) findViewById(R.id.tv_private_chat);
        listView = (PullListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        listView.setOnRefreshListener(this);
        if (!flag) {
            listView.onStartHeadRefresh();
        }
    }

    private void getData() {
        String path = "/nuke.php?";
        HttpParams params = new HttpParams();
        params.put("__lib", "message");
        params.put("__act", "message");
        params.put("act", "list");
        params.put("page", page);
        params.put("lite", "js");
        String url = GlobalConst.BASE_URL + path + params.toString();
        mQueue.add(new MessageRequest(Request.Method.GET, url,
                new Response.Listener<List<Message>>() {
                    @Override
                    public void onResponse(List<Message> response) {
                        if (page == 1) {
                            messages.clear();
                        }
                        if(response.size()==0){
                            nextPage = false;
                            Toast.makeText(MessageActivity.this,"已经是最后一页",Toast.LENGTH_SHORT).show();
                        }else {
                            messages.addAll(response);
                            adapter.notifyDataSetChanged();
                        }
                        flag = true;
                        listView.onCompleteRefresh();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        listView.onCompleteRefresh();
                    }
                }));
    }


    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.action_return:
                finishForAnimation();
                break;
            case R.id.tv_private_chat:
                //私聊
                if (index == 1) {
                    index = 0;
                    tvMessage.setTextColor(getResources().getColor(R.color.un_select_text));
                    tvPrivate.setTextColor(getResources().getColor(R.color.select_text));
                    Animation animation = new TranslateAnimation(0, GlobalConst.SCREEN_WIDTH, 0, 0);
                    animation.setDuration(300);
                    animation.setAnimationListener(this);
                    listView.startAnimation(animation);
                }
                break;
            case R.id.tv_message:
                //短信
                if (index == 0) {
                    index = 1;
                    tvMessage.setTextColor(getResources().getColor(R.color.select_text));
                    tvPrivate.setTextColor(getResources().getColor(R.color.un_select_text));
                    Animation animation = new TranslateAnimation(GlobalConst.SCREEN_WIDTH, 0, 0, 0);
                    animation.setDuration(300);
                    listView.setVisibility(View.VISIBLE);
                    listView.startAnimation(animation);
                    if (!flag) {
                        getData();
                    }
                }
                break;
            case R.id.iv_check_bg:
                //将所有消息标记为已读
                break;
            case R.id.iv_write:
                //写短消息
                break;
        }
    }

    /**
     * 动画监听
     */
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        listView.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }



    /**
     * 下拉刷新
     */
    @Override
    public void pullDownToRefresh() {
        page = 1;
        getData();
    }

    /**
     * 上拉加载
     */
    @Override
    public void pullUpToRefresh() {
        if (!nextPage) {
            Toast.makeText(MessageActivity.this, "已经是最后一页", Toast.LENGTH_SHORT).show();
            listView.setSelection(messages.size() - 2);
        } else {
            page++;
            getData();
        }
    }

    /**
     * 点击item事件监听
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, MessageContentActivity.class);
        intent.putExtra("mid", (int) id);
        startActivity(intent);
    }

    private class MessageAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return messages.size();
        }

        @Override
        public Object getItem(int position) {
            return messages.get(position);
        }

        @Override
        public long getItemId(int position) {
            return messages.get(position).getMid();
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            ViewHolder holder;
            if (view == null) {
                view = View.inflate(MessageActivity.this, R.layout.activity_message_item, null);
                holder = new ViewHolder();
                holder.subject = (TextView) view.findViewById(R.id.subject);
                holder.last_poster = (TextView) view.findViewById(R.id.last_poster);
                holder.last_modify = (TextView) view.findViewById(R.id.last_modify);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            Message message = messages.get(position);
            holder.subject.setText(message.getSubject());
            String last_username = message.getLast_from_username();
            if (last_username.length() == 0) {
                last_username = "#SYSTEM#";
            }
            last_username = last_username + "(" + message.getPosts() + ")";
            holder.last_poster.setText(last_username);
            holder.last_modify.setText(TimeUtils.formatStandardTime(message.getLast_modify() * 1000));
            return view;
        }
    }

    private class ViewHolder {
        TextView subject;
        TextView last_poster;
        TextView last_modify;
    }
}
